//
//  BaseWebViewController.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/11/24.
//  Copyright © 2015年 szb. All rights reserved.
//

#import "BaseWebViewController.h"
#import "MBProgressHUD+MJ.h"
#import "AccountTool.h"
#import "BPLoginViewController.h"
#import "AppDelegate.h"

@interface BaseWebViewController ()

@end

@implementation BaseWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _baseWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 10)];
    
    [self moveCookies];
    
    
    _baseWebView.opaque = NO;
    _baseWebView.delegate = self;
    _baseWebView.detectsPhoneNumbers = NO;
    _baseWebView.backgroundColor = [UIColor whiteColor];
    [_baseWebView.scrollView setShowsHorizontalScrollIndicator:NO];
    [_baseWebView.scrollView setShowsVerticalScrollIndicator:NO];
    NSString *path = [[[NSBundle mainBundle] bundlePath]  stringByAppendingPathComponent:self.webName];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
    [_baseWebView loadRequest:request];
    
    [self.view addSubview:_baseWebView];
    // Do any additional setup after loading the view.
}

- (void)moveCookies
{
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])
    {
        [storage deleteCookie:cookie];
    }
    [[NSURLCache  sharedURLCache] removeAllCachedResponses];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    // 打印异常
    self.context.exceptionHandler =
    ^(JSContext *context, JSValue *exceptionValue)
    {
        context.exception = exceptionValue;
        BPLog(@"%@", exceptionValue);
    };
    
    self.context[@"api"] = self;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    // 打印异常
    self.context.exceptionHandler =
    ^(JSContext *context, JSValue *exceptionValue)
    {
        context.exception = exceptionValue;
        BPLog(@"%@", exceptionValue);
    };
    
    self.context[@"api"] = self;

}

- (void)endRefresh
{

}

- (void)pushAUiViewController:(NSString *)webName
{

}

- (void)pushINT:(int)index
{
    NSLog(@"%d",index);
}

- (void)log:(NSString *)str
{
    NSLog(@"%@",str);
}

- (void)logObject:(NSDictionary *)obj
{
    NSLog(@"%@",obj);
}

- (void)graduateDeskCalendar
{

}

- (void)showCloudPicList
{

}

- (void)finish
{
    
}

- (void)setConfig:(NSString *)key :(NSString *)value
{
    [[NSUserDefaults standardUserDefaults] setValue:value forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)getConfig:(NSString *)key
{
    return [[NSUserDefaults standardUserDefaults] valueForKey:key];
}

- (void)showToast:(NSString *)message
{
    [MBProgressHUD showSuccess:message];
}

- (void)showMessage:(NSString *)message
{
    [MBProgressHUD showMessage3:message];
}

- (void)showSuccess:(NSString *)message
{
    [MBProgressHUD showSuccess:message];
}

- (void)showError:(NSString *)message
{
    [MBProgressHUD showError:message];
}

- (NSString *)getUid:(NSString *)uid
{
    return [AccountTool account].uid;
}

- (void)backToFunction:(NSString *)method :(NSDictionary *)data
{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
